package com.rtx.edi.platform.asyntask.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.asyntask.dto.PlTaskQueueDTO;
import com.rtx.edi.model.asyntask.po.PlTaskQueue;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.asyntask.service.PlTaskQueueService;

import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 异步消息队列表 前端控制器
 * </p>
 *
 * @author zcw
 * @since 2023-03-19
 */
@Api(tags = {"异步任务队列"})
@RestController
@RequestMapping("/v1/edi/asyntask")
public class PlTaskQueueController {

	@Autowired
	private PlTaskQueueService PlTaskQueueService;

	/**
	 * 获取异步消息队列列表
	 * @return
	 */
	@ApiOperation(value = "异步消息队列列表", notes = "分页获取异步消息队列列表")
	@PostMapping("selectPlTaskQueueList")
	public EdiResponse selectPlTaskQueueList(@RequestBody(required = false) PlTaskQueueDTO dto){
		Page<PlTaskQueue> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
		PlTaskQueue po = new PlTaskQueue();
		BeanUtils.copyProperties(dto, po);
		PlTaskQueueService.selectPlTaskQueueList(pageParam,po);
		return EdiResponse.success().data("page",pageParam);
	}
	
	@ApiOperation(value = "获取异步消息队列", notes = "获取异步消息队列列表")
	@PostMapping("selectPlTaskQueue")
	public EdiResponse selectPlTaskQueue(){
		List<PlTaskQueue> list = PlTaskQueueService.selectPlTaskQueueList();
		List<Map> item = new ArrayList<>();
		for(PlTaskQueue info:list) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", info.getId());
			map.put("companyId", info.getCompanyId());
			map.put("applicationId", info.getApplicationLinkId());
			item.add(map);
		}
		return EdiResponse.success().data("item",item);
	}

	/**
	 * 根据ID获取异步消息队列
	 * @param PlTaskQueue
	 * @return
	 */
	@ApiOperation(value = "查询异步消息队列", notes = "根据ID查询异步消息队列")
	@PostMapping("selectPlTaskQueueById")
	public EdiResponse selectPlTaskQueueById(@RequestBody PlTaskQueue dto){
		return EdiResponse.success().data("item",PlTaskQueueService.getById(dto.getId()));
	}
	/**
	 * 根据id删除
	 * @param PlTaskQueue
	 * @return
	 */
	@ApiOperation(value = "删除异步消息队列", notes = "根据id删除异步消息队列", httpMethod = "POST")
	@PostMapping("deletePlTaskQueueById")
	public EdiResponse deletePlTaskQueueById(@RequestBody PlTaskQueue dto) {
		return PlTaskQueueService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
	}
	
	/**
	 * 新增异步消息队列
	 * @param PlTaskQueue
	 * @return
	 */
	@ApiOperation(value = "新增异步消息队列")
	@PostMapping("addPlTaskQueue")
	public EdiResponse addPlTaskQueue(@RequestBody PlTaskQueueDTO dto){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		String companyid =  (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");


		PlTaskQueue po = new PlTaskQueue();
		BeanUtils.copyProperties(dto, po);
		po.setCompanyId(Long.parseLong(companyid));
		po.setTaskNo(IdUtil.getSnowflakeNextIdStr());
		return PlTaskQueueService.save(po)  ? EdiResponse.success() : EdiResponse.failed();
	}

	/**
	 * 根据ID修改异步消息队列
	 * @return
	 */
	@ApiOperation(value = "修改异步消息队列", notes = "根据ID修改异步消息队列")
	@PostMapping("editPlTaskQueueById")
	public EdiResponse editPlTaskQueueById(@RequestBody PlTaskQueueDTO dto){
		PlTaskQueue po = new PlTaskQueue();
		BeanUtils.copyProperties(dto, po);
		return PlTaskQueueService.updateById(po) ? EdiResponse.success() : EdiResponse.failed();
	}
}
